package com.modai.springcloud.entities;

import com.modai.springcloud.service.HystrixOrderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ：modai
 * @date ：Created in 2020-12-7 20:09
 * @description：
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "global_fallback")
public class HystrixOrderController {

    @Resource
    private HystrixOrderService hystrixOrderService;

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String responseOk(@PathVariable("id") int id){
        return hystrixOrderService.responseOk(id);
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")  //3秒钟以内就是正常的业务逻辑
//    })
    @HystrixCommand
    public String responseTimeout(@PathVariable("id") int id){
        int s=10/0;
        return hystrixOrderService.responseTimeout(id);
    }
    private String paymentTimeOutFallbackMethod(int id){
        return "这里是客户端。sorry, API调用超时或错误，查查看吧。👁👁,线程为："+Thread.currentThread().getName();
    }

    private String global_fallback(){
        return "Global服务降级被调用。";
    }
}
